<? 
require("adm_header.php"); 
writeCommonAdminTable("Import Tool",1);

echo "<b>Important</b><br /><ol><li>Your other (old) install must be in the same database as this one.</li><li>Your old db must have prefix 'phpclass_'.</li><li>Empty ALL tables in <b>this</b> new database for data (do not delete the tables! Only empty them!).</li></ol>";
echo "<form method='post' action='import_utf8.php'>";
echo " <input type='submit' name='start' value='Start'>";
echo "</form>";
$lDryTest=1;
$lStart=getParam("start","");

if ($lStart)
{
	$lTableArray=getTables();
	
	foreach ($lTableArray as $lTableName) // Loop through all tables
	{
		$lResInsert=mysql_connect("localhost","$set_mysql_user","$set_mysql_pass");
		$lResSelect=mysql_select_db("$set_mysql_base",$lResInsert);
		$lSql="show fields from " . $lTableName  . " from `$set_mysql_base`";
		$lResFields=mysql_query($lSql,$lResInsert);
		$lTableNameNew=ereg_replace("phpclass_",$set_table_prefix,$lTableName);
		
		$lColumnNames="";
		$lColumnValues="";
		while ($row_fields=mysql_fetch_array($lResFields))
		{
				$lFieldArray[$i]["Table"]=$row[0];
				$lFieldArray[$i]["Field"]=$row_fields["Field"];
				$lFieldArray[$i]["Type"]=$row_fields["Type"];
				$lFieldArray[$i]["Extra"]=$row_fields["Extra"];
				//echo $row_fields["Field"] . "<br />";
				
				
				if (ereg("e_",$row_fields["Field"]) || ereg("opt_",$row_fields["Field"]) | ereg("lang_",$row_fields["Field"]))
				{
					$lResInsertExtra=mysql_connect("localhost","$set_mysql_user","$set_mysql_pass");
					$lResSelectExtra=mysql_select_db("$set_mysql_base",$lResInsert);
					mysql_query("SET NAMES 'utf8'",$lResInsert);
					
					$lSql="alter table $lTableNameNew add ".$row_fields["Field"] ." VarChar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL;";
					@mysql_query($lSql,$lResInsertExtra);
					@mysql_close($lResSelectExtra);
				}
				
				
				
				
				$i++;
				
				$lColumnNames.=$row_fields["Field"] . ","; 	
		}
		$lColumnNames=substr($lColumnNames,0,-1);
		if ($lTableName!="phpclass_stat")
		{
			$lSql="select * from $lTableName";
			$lResGetOld=mysql_connect("localhost","$set_mysql_user","$set_mysql_pass");
			$lResSelectOld=mysql_select_db("$set_mysql_base",$lResGetOld);
			mysql_query("SET NAMES 'latin1'",$lResGetOld);
			$lResFetch=mysql_query($lSql,$lResGetOld);
			
			while ($lRow=mysql_fetch_array($lResFetch))
			{
				//$lColumnNames="";
				$lColumnValues="";
				
				$lColumnNamesArray=split(",",$lColumnNames);
				foreach ($lColumnNamesArray as $lColHeading) 
				{
					
					
					if ($lColHeading!="")
					{
						
						$lVal=$lRow["$lColHeading"];
						//$lVal=ereg_replace("\?","&#63;",$lVal);
						$lVal=ereg_replace("'","\'",$lVal);
						
						$lColumnValues.="'" .$lVal . "',";	
					}
				}
				
				$lColumnValues=substr($lColumnValues,0,-1);
				
				$lSql="insert into " . $lTableNameNew . ""  ." ($lColumnNames) values ($lColumnValues);";
				
				echo "<pre>" . $lSql . "</pre><br />";
				
				$lNum++;
				$lResInsert=mysql_connect("localhost","$set_mysql_user","$set_mysql_pass");
				$lResSelect=mysql_select_db("$set_mysql_base",$lResInsert);
				mysql_query("SET NAMES 'utf8'",$lResInsert);
				mysql_query($lSql,$lResInsert);
				mysql_close($lResInsert);
							
	
				
			}
			
					
					
				//$lSql="insert into $lTableName ($lColumnName) values ($lColumnValue)";
				//echo $lSql. "<br />";
		}
		
	}
	echo "<p>Complete! $lNum queries done.</p>";
}

require("adm_footer.php"); 
?>